﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using Assist;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class HYDuctFittingConnect : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			this.uiApp = commandData.Application;
			this.doc = this.uiApp.ActiveUIDocument.Document;
			this.sel = this.uiApp.ActiveUIDocument.Selection;
			RevitVersionFuncs.ClearSelection(this.sel);
			List<ViewType> viewTypes = YJKRevitTools.ListViewNeed(2);
			if (!YJKRevitTools.ViewCheck(this.uiApp.ActiveUIDocument, viewTypes))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			if (HYDuctFittingConnect.hWndRevit == null)
			{
				HYDuctFittingConnect.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
			}
			ConnDuctFrom connDuctFrom = new ConnDuctFrom(this.doc);
			connDuctFrom.Show(HYDuctFittingConnect.hWndRevit);
			int wMsg = 7;
			HYDuctFittingConnect.SendMessage(HYDuctFittingConnect.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
			new ConnectDucs(this.uiApp, connDuctFrom).Execute();
			connDuctFrom.Close();
			return Autodesk.Revit.UI.Result.Succeeded;
		}

		private string ConnDuctFitting(IList<Duct> ducts, IList<XYZ> xyzs, Selection sel, ConnSelectStyle connStyle, ConnectDuctStyle fittingStyle, bool flag)
		{
			string result = "";
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ISelectionFilter selectionFilter = new DuctFilter();
			FamilyInstance familyInstance = null;
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			int ductShape = connectDuctManage.GetDuctShape(ducts[0]);
			if (ducts.Count < 1)
			{
				return "没有选中风管，请选择一根风管！";
			}
			FamilySymbol familySymbol = null;
			Element element = null;
			List<string> list = new List<string>();
			switch (connStyle)
			{
			case ConnSelectStyle.Elbow:
			{
				string text;
				if (ductShape == 1)
				{
					text = "选择第二个风管(矩形)，与第一个风管夹角大于等于90°，小于180°";
				}
				else if (ductShape == 0)
				{
					text = "选择第二个风管(圆形)，与第一个风管夹角大于等于90°，小于180°";
				}
				else
				{
					if (ductShape != 2)
					{
						return "风管类型有误，无法连接！";
					}
					text = "选择第二个风管(椭圆形)，与第一个风管夹角大于等于90°，小于180°";
				}
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text);
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					if ((fittingStyle == ConnectDuctStyle.ArcElbow || fittingStyle == ConnectDuctStyle.DiversionElbow || fittingStyle == ConnectDuctStyle.SilencingElbow) && ductShape != 1)
					{
						return "弯头截面与风管截面不匹配！";
					}
					if ((fittingStyle == ConnectDuctStyle.ArcElbowRound || fittingStyle == ConnectDuctStyle.ShrimpElbowRound) && ductShape != 0)
					{
						return "弯头截面与风管截面不匹配！";
					}
					if (fittingStyle == ConnectDuctStyle.DiversionElbow || fittingStyle == ConnectDuctStyle.SilencingElbow)
					{
						if (ducts.Count != 2 || xyzs.Count != 2)
						{
							return "请选择两条风管进行弯头连接！";
						}
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
						YJKLine hyline = new YJKLine(hypiping.EndConnector.Origin, hypiping.StartConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping2.EndConnector.Origin, hypiping2.StartConnector.Origin);
						if (!hyline.IsVertical(hyline2.Line))
						{
							return "请选择弧形弯头连接\n或圆形管道的连接方式！";
						}
					}
					connectDuctManage = new ConnectDuctManage();
					list = connectDuctManage.GetFittingNames(fittingStyle);
					if (list.Count != 1)
					{
						return "获取弯头族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(list[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewElbowFitting(ducts, xyzs, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Lateral:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二个风管,与第一根水管为90°夹角");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (ductShape == 1)
				{
					fittingStyle = ConnectDuctStyle.Side;
				}
				if (ductShape == 0)
				{
					fittingStyle = ConnectDuctStyle.SideRound;
				}
				if (fittingStyle == ConnectDuctStyle.Side && ductShape != 1)
				{
					return "管件截面与风管截面不匹配！";
				}
				if (fittingStyle == ConnectDuctStyle.SideRound && ductShape != 0)
				{
					return "管件截面与风管截面不匹配！";
				}
				if (flag)
				{
                    int numberOfRules = ducts[0].DuctType.RoutingPreferenceManager.GetNumberOfRules((RoutingPreferenceRuleGroupType)2);
					for (int i = 0; i < numberOfRules; i++)
					{
						if (ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, i).MEPPartId != null)
						{
							element = this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, i).MEPPartId);
						}
						if (element != null)
						{
							UnifiedModified.ActivateFamilySymbol(element as FamilySymbol);
							FamilyInstance familyInstance2 = this.doc.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), element as FamilySymbol, element, this.doc.ActiveView.GenLevel, 0);
                            if ((int)(familyInstance2.MEPModel as MechanicalFitting).PartType == 11 || (int)(familyInstance2.MEPModel as MechanicalFitting).PartType == 10)
							{
								familySymbol = (element as FamilySymbol);
								this.doc.Delete(familyInstance2.Id);
								break;
							}
							this.doc.Delete(familyInstance2.Id);
						}
					}
					if (familySymbol == null)
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					connectDuctManage = new ConnectDuctManage();
					list = connectDuctManage.GetFittingNames(fittingStyle);
					if (list.Count != 1)
					{
						return "获取侧连接族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(list[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewTakeOffFitting(ducts, xyzs, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Tee:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择出口风管");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择支管");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的三根风管的截面类型必须相同！";
				}
				if ((fittingStyle == ConnectDuctStyle.RoundAngleTee || fittingStyle == ConnectDuctStyle.RightAngleTee || fittingStyle == ConnectDuctStyle.IntubationTee || fittingStyle == ConnectDuctStyle.TShapeTee || fittingStyle == ConnectDuctStyle.YShapeTee || fittingStyle == ConnectDuctStyle.CommonTee) && ductShape != 1)
				{
					return "三通截面与风管截面不匹配！";
				}
				if ((fittingStyle == ConnectDuctStyle.CommonTeeRound || fittingStyle == ConnectDuctStyle.RightAngleTeeRound || fittingStyle == ConnectDuctStyle.SlantingTeeRound) && ductShape != 0)
				{
					return "三通截面与风管截面不匹配！";
				}
				List<string> list2 = new List<string>();
				if (flag)
				{
                    int numberOfRules2 = ducts[0].DuctType.RoutingPreferenceManager.GetNumberOfRules((RoutingPreferenceRuleGroupType)2);
					for (int j = 0; j < numberOfRules2; j++)
					{
						if (ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, j).MEPPartId != null)
						{
							element = this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, j).MEPPartId);
						}
						if (element != null)
						{
							UnifiedModified.ActivateFamilySymbol(element as FamilySymbol);
							FamilyInstance familyInstance3 = this.doc.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), element as FamilySymbol, element, this.doc.ActiveView.GenLevel, 0);
                            if ((int)(familyInstance3.MEPModel as MechanicalFitting).PartType == 6)
							{
								familySymbol = (element as FamilySymbol);
								this.doc.Delete(familyInstance3.Id);
								break;
							}
							this.doc.Delete(familyInstance3.Id);
						}
					}
					if (familySymbol == null)
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					list2.Add((element as FamilySymbol).Family.Name);
					list2.Add((element as FamilySymbol).Family.Name);
					list2.Add((element as FamilySymbol).Family.Name);
					list2 = null;
				}
				else
				{
					list2 = connectDuctManage.GetFittingNames(fittingStyle);
				}
				switch (fittingStyle)
				{
				case ConnectDuctStyle.TShapeTee:
				case ConnectDuctStyle.YShapeTee:
					if (YJKRevitTools.IsRiserDuct(ducts[0]))
					{
						familyInstance = hyductConnect.NewTeeFittingHF(ducts, xyzs, list2, ref result);
						goto IL_78E;
					}
					familyInstance = hyductConnect.NewTeeFittingF(ducts, xyzs, list2, ref result);
					goto IL_78E;
				case ConnectDuctStyle.RoundAngleTee:
				case ConnectDuctStyle.RightAngleTee:
				case ConnectDuctStyle.IntubationTee:
					break;
				case ConnectDuctStyle.RReduceAngleTee:
					goto IL_78E;
				case ConnectDuctStyle.CommonTee:
					goto IL_780;
				default:
					if (fittingStyle - ConnectDuctStyle.RightAngleTeeRound > 1)
					{
						if (fittingStyle != ConnectDuctStyle.CommonTeeRound)
						{
							goto IL_78E;
						}
						goto IL_780;
					}
					break;
				}
				if (YJKRevitTools.IsRiserDuct(ducts[2]))
				{
					familyInstance = hyductConnect.NewTeeFittingHT(ducts, xyzs, list2, ref result);
					goto IL_78E;
				}
				familyInstance = hyductConnect.NewTeeFittingT(ducts, xyzs, list2, ref result);
				goto IL_78E;
				IL_780:
				familyInstance = hyductConnect.NewTeeFittingY(ducts, xyzs, list2, ref result);
				IL_78E:
				if ((familyInstance != null && list2 != null) || list2 == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Cross:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二根风管，与第一根风管在同一直线上");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (fittingStyle != ConnectDuctStyle.SlantingCrossRound)
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第三根风管，与第一、二根风管垂直");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第四根风管，与第三个风管在同一直线上，与第一、二根风管垂直");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
				}
				else
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第三根风管，与第一根风管间夹角应在90°-180°");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第四根风管，与第一根风管间夹角应在90°-180°");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
				}
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的四根风管的截面类型必须相同！";
				}
				if ((fittingStyle == ConnectDuctStyle.RoundAngleCross || fittingStyle == ConnectDuctStyle.RightAngleCross || fittingStyle == ConnectDuctStyle.IntubationCross) && ductShape != 1)
				{
					return "四通截面与风管截面不匹配！";
				}
				if ((fittingStyle == ConnectDuctStyle.SlantingCrossRound || fittingStyle == ConnectDuctStyle.IntubationCrossRound || fittingStyle == ConnectDuctStyle.RoundAngleCorssRound) && ductShape != 0)
				{
					return "四通截面与风管截面不匹配！";
				}
				List<string> list3 = new List<string>();
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)3, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					list3 = null;
				}
				else
				{
					list3 = connectDuctManage.GetFittingNames(fittingStyle);
				}
				if (fittingStyle <= ConnectDuctStyle.IntubationCross)
				{
					if (fittingStyle != ConnectDuctStyle.RoundAngleCross && fittingStyle - ConnectDuctStyle.RightAngleCross > 1)
					{
						goto IL_98A;
					}
				}
				else if (fittingStyle - ConnectDuctStyle.RoundAngleCorssRound > 1)
				{
					if (fittingStyle != ConnectDuctStyle.SlantingCrossRound)
					{
						goto IL_98A;
					}
					familyInstance = hyductConnect.NewCrossFittingY(ducts, xyzs, list3, ref result);
					goto IL_98A;
				}
				familyInstance = hyductConnect.NewCrossFitting(ducts, xyzs, list3, ref result);
				IL_98A:
				if ((familyInstance != null && list3 != null) || list3 == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Reduce:
			{
				string text;
				if (ductShape == 1)
				{
					text = "选择第二个风管(矩形)，与第一个风管在同一直线上";
				}
				else if (ductShape == 0)
				{
					text = "选择第二个风管(圆形)，与第一个风管在同一直线上";
				}
				else
				{
					if (ductShape != 2)
					{
						return "风管类型有误，无法连接！";
					}
					text = "选择第二个风管(椭圆形)，与第一个风管在同一直线上";
				}
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text);
				ducts.Add(this.doc.GetElement(reference) as Duct);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (this.DuctSizeEqual(ducts))
				{
					return "两根风管的尺寸相同，不能进行变径连接！";
				}
				if (ductShape == 1)
				{
					fittingStyle = ConnectDuctStyle.Variable;
				}
				if (ductShape == 0)
				{
					fittingStyle = ConnectDuctStyle.VariableRound;
				}
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)4, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					if (fittingStyle == ConnectDuctStyle.Variable && ductShape != 1)
					{
						return "变径截面与风管截面不匹配！";
					}
					if (fittingStyle == ConnectDuctStyle.VariableRound && ductShape != 0)
					{
						return "变径截面与风管截面不匹配！";
					}
					connectDuctManage = new ConnectDuctManage();
					list = connectDuctManage.GetFittingNames(fittingStyle);
					if (list.Count != 1)
					{
						return "获取变径族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(list[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewTransitionFitting(ducts, true, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Round:
			{
				string text;
				if (ductShape == 1)
				{
					text = "选择第二个风管(圆形)，与第一个风管在同一直线上";
				}
				else
				{
					if (ductShape != 0 && ductShape != 2)
					{
						return "风管类型有误，无法连接！";
					}
					text = "选择第二个风管(矩形)，与第一个风管在同一直线上";
				}
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text);
				ducts.Add(this.doc.GetElement(reference) as Duct);
				if (this.CheckDuctShape(ducts))
				{
					result = "选择的两根风管，必须是一根为矩形风管，一根为圆形风管！";
					return result;
				}
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)7, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					connectDuctManage = new ConnectDuctManage();
					list = connectDuctManage.GetFittingNames(fittingStyle);
					if (list.Count != 1)
					{
						return "获取天圆地方族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(list[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewTransitionFitting(ducts, true, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Back:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二个风管,与第一根水管平行");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (ductShape == 1)
				{
					fittingStyle = ConnectDuctStyle.BackForth;
				}
				if (ductShape == 0)
				{
					fittingStyle = ConnectDuctStyle.BackForthRound;
				}
				if (fittingStyle == ConnectDuctStyle.BackForth && ductShape != 1)
				{
					return "弯头截面与风管截面不匹配！";
				}
				if (fittingStyle == ConnectDuctStyle.BackForthRound && ductShape != 0)
				{
					return "弯头截面与风管截面不匹配！";
				}
				if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)4, 0).MEPPartId) is FamilySymbol))
				{
					return "布管配置中变径配置为空，请检查Revit的族配置";
				}
				connectDuctManage = new ConnectDuctManage();
				list = connectDuctManage.GetFittingNames(fittingStyle);
				if (list.Count != 1)
				{
					return "获取弯头族名称失败！";
				}
				familySymbol = connectDuctManage.GetFamilySymbol(list[0], this.doc, ref result);
				if (familySymbol == null)
				{
					return result;
				}
				IList<FamilyInstance> list4 = hyductConnect.NewLHWFitting(ducts, true, familySymbol, ref result);
				if (list4 != null && list4.Count > 0)
				{
					this.doc.Regenerate();
				}
				break;
			}
			}
			return result;
		}

		private string ConnectDuctFittingNew(IList<Duct> ducts, IList<XYZ> xyzs, Selection sel, ConnSelectStyle connStyle, ConnectDuctStyle fittingStyle, bool flag)
		{
			string result = "";
			HYDuctConnect hyductConnect = new HYDuctConnect();
			ISelectionFilter selectionFilter = new DuctFilter();
			FamilyInstance familyInstance = null;
			ConnectDuctManage connectDuctManage = new ConnectDuctManage();
			int ductShape = connectDuctManage.GetDuctShape(ducts[0]);
			if (ducts.Count < 1)
			{
				return "没有选中风管，请选择一根风管！";
			}
			FamilySymbol familySymbol = null;
			Element element = null;
			switch (connStyle)
			{
			case ConnSelectStyle.Elbow:
			{
				string text;
				if (ductShape == 1)
				{
					text = "选择第二个风管(矩形)，与第一个风管夹角大于等于90°，小于180°";
				}
				else if (ductShape == 0)
				{
					text = "选择第二个风管(圆形)，与第一个风管夹角大于等于90°，小于180°";
				}
				else
				{
					if (ductShape != 2)
					{
						return "风管类型有误，无法连接！";
					}
					text = "选择第二个风管(椭圆形)，与第一个风管夹角大于等于90°，小于180°";
				}
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text);
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)1, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					if ((fittingStyle == ConnectDuctStyle.ArcElbow || fittingStyle == ConnectDuctStyle.DiversionElbow || fittingStyle == ConnectDuctStyle.SilencingElbow) && ductShape != 1)
					{
						return "弯头截面与风管截面不匹配！";
					}
					if ((fittingStyle == ConnectDuctStyle.ArcElbowRound || fittingStyle == ConnectDuctStyle.ShrimpElbowRound) && ductShape != 0)
					{
						return "弯头截面与风管截面不匹配！";
					}
					if (fittingStyle == ConnectDuctStyle.DiversionElbow || fittingStyle == ConnectDuctStyle.SilencingElbow)
					{
						if (ducts.Count != 2 || xyzs.Count != 2)
						{
							return "请选择两条风管进行弯头连接！";
						}
						YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
						YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
						YJKLine hyline = new YJKLine(hypiping.EndConnector.Origin, hypiping.StartConnector.Origin);
						YJKLine hyline2 = new YJKLine(hypiping2.EndConnector.Origin, hypiping2.StartConnector.Origin);
						if (!hyline.IsVertical(hyline2.Line))
						{
							return "请选择弧形弯头连接\n或圆形管道的连接方式！";
						}
					}
					connectDuctManage = new ConnectDuctManage();
					List<string> fittingNames = connectDuctManage.GetFittingNames(fittingStyle);
					if (fittingNames.Count != 1)
					{
						return "获取弯头族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewElbowFitting(ducts, xyzs, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Lateral:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二个风管,与第一根水管为90°夹角");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (ductShape == 1)
				{
					fittingStyle = ConnectDuctStyle.Side;
				}
				if (ductShape == 0)
				{
					fittingStyle = ConnectDuctStyle.SideRound;
				}
				if (fittingStyle == ConnectDuctStyle.Side && ductShape != 1)
				{
					return "管件截面与风管截面不匹配！";
				}
				if (fittingStyle == ConnectDuctStyle.SideRound && ductShape != 0)
				{
					return "管件截面与风管截面不匹配！";
				}
				if (flag)
				{
                    int numberOfRules = ducts[0].DuctType.RoutingPreferenceManager.GetNumberOfRules((RoutingPreferenceRuleGroupType)2);
					for (int i = 0; i < numberOfRules; i++)
					{
						if (ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, i).MEPPartId != null)
						{
							element = this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, i).MEPPartId);
						}
						if (element != null)
						{
							UnifiedModified.ActivateFamilySymbol(element as FamilySymbol);
							FamilyInstance familyInstance2 = this.doc.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), element as FamilySymbol, element, this.doc.ActiveView.GenLevel, 0);
                            if ((int)(familyInstance2.MEPModel as MechanicalFitting).PartType == 11 || (int)(familyInstance2.MEPModel as MechanicalFitting).PartType == 10)
							{
								familySymbol = (element as FamilySymbol);
								this.doc.Delete(familyInstance2.Id);
								break;
							}
							this.doc.Delete(familyInstance2.Id);
						}
					}
					if (familySymbol == null)
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					connectDuctManage = new ConnectDuctManage();
					List<string> fittingNames = connectDuctManage.GetFittingNames(fittingStyle);
					if (fittingNames.Count != 1)
					{
						return "获取侧连接族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewTakeOffFitting(ducts, xyzs, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Tee:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择出口风管");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择支管");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的三根风管的截面类型必须相同！";
				}
				if ((fittingStyle == ConnectDuctStyle.RoundAngleTee || fittingStyle == ConnectDuctStyle.RightAngleTee || fittingStyle == ConnectDuctStyle.IntubationTee || fittingStyle == ConnectDuctStyle.TShapeTee || fittingStyle == ConnectDuctStyle.YShapeTee || fittingStyle == ConnectDuctStyle.CommonTee) && ductShape != 1)
				{
					return "三通截面与风管截面不匹配！";
				}
				if ((fittingStyle == ConnectDuctStyle.CommonTeeRound || fittingStyle == ConnectDuctStyle.RightAngleTeeRound || fittingStyle == ConnectDuctStyle.SlantingTeeRound) && ductShape != 0)
				{
					return "三通截面与风管截面不匹配！";
				}
				List<string> list = new List<string>();
				if (flag)
				{
                    int numberOfRules2 = ducts[0].DuctType.RoutingPreferenceManager.GetNumberOfRules((RoutingPreferenceRuleGroupType)2);
					for (int j = 0; j < numberOfRules2; j++)
					{
						if (ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, j).MEPPartId != null)
						{
							element = this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)2, j).MEPPartId);
						}
						if (element != null)
						{
							UnifiedModified.ActivateFamilySymbol(element as FamilySymbol);
							FamilyInstance familyInstance3 = this.doc.Create.NewFamilyInstance(new XYZ(0.0, 0.0, 0.0), element as FamilySymbol, element, this.doc.ActiveView.GenLevel, 0);
							MechanicalFitting mechanicalFitting = familyInstance3.MEPModel as MechanicalFitting;
                            if (mechanicalFitting != null && (int)mechanicalFitting.PartType == 6)
							{
								familySymbol = (element as FamilySymbol);
								this.doc.Delete(familyInstance3.Id);
								break;
							}
							this.doc.Delete(familyInstance3.Id);
						}
					}
					if (familySymbol == null)
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					list.Add((element as FamilySymbol).Family.Name);
					list.Add((element as FamilySymbol).Family.Name);
					list.Add((element as FamilySymbol).Family.Name);
					list = null;
				}
				else
				{
					list = connectDuctManage.GetFittingNames(fittingStyle);
				}
				switch (fittingStyle)
				{
				case ConnectDuctStyle.TShapeTee:
					if (YJKRevitTools.IsRiserDuct(ducts[0]))
					{
						familyInstance = hyductConnect.NewTeeFittingHF(ducts, xyzs, list, ref result);
						goto IL_7C8;
					}
					familyInstance = hyductConnect.NewTeeFittingF(ducts, xyzs, list, ref result);
					goto IL_7C8;
				case ConnectDuctStyle.RoundAngleTee:
					list = connectDuctManage.GetRoundAngleTeeNames();
					hyductConnect.IsConnecteddByRoundAngleTee(ducts, xyzs, list, ref result);
					goto IL_7C8;
				case ConnectDuctStyle.RReduceAngleTee:
					goto IL_7C8;
				case ConnectDuctStyle.RightAngleTee:
				case ConnectDuctStyle.IntubationTee:
					break;
				case ConnectDuctStyle.YShapeTee:
					list = connectDuctManage.GetYShapeTeeNames();
					hyductConnect.IsConnecteddByYShapeTee(ducts, xyzs, list, ref result);
					goto IL_7C8;
				case ConnectDuctStyle.CommonTee:
					goto IL_7BA;
				default:
					if (fittingStyle - ConnectDuctStyle.RightAngleTeeRound > 1)
					{
						if (fittingStyle != ConnectDuctStyle.CommonTeeRound)
						{
							goto IL_7C8;
						}
						goto IL_7BA;
					}
					break;
				}
				if (YJKRevitTools.IsRiserDuct(ducts[2]))
				{
					familyInstance = hyductConnect.NewTeeFittingHT(ducts, xyzs, list, ref result);
					goto IL_7C8;
				}
				familyInstance = hyductConnect.NewTeeFittingT(ducts, xyzs, list, ref result);
				goto IL_7C8;
				IL_7BA:
				familyInstance = hyductConnect.NewTeeFittingY(ducts, xyzs, list, ref result);
				IL_7C8:
				if ((familyInstance != null && list != null) || list == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Cross:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二根风管，与第一根风管在同一直线上");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				xyzs.Add(reference.GlobalPoint);
				if (fittingStyle != ConnectDuctStyle.SlantingCrossRound)
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第三根风管，与第一、二根风管垂直");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第四根风管，与第三个风管在同一直线上，与第一、二根风管垂直");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
				}
				else
				{
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第三根风管，与第一根风管间夹角应在90°-180°");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
					reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第四根风管，与第一根风管间夹角应在90°-180°");
					ducts.Add(this.doc.GetElement(reference) as Duct);
					xyzs.Add(reference.GlobalPoint);
				}
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的四根风管的截面类型必须相同！";
				}
				if ((fittingStyle == ConnectDuctStyle.RoundAngleCross || fittingStyle == ConnectDuctStyle.RightAngleCross || fittingStyle == ConnectDuctStyle.IntubationCross) && ductShape != 1)
				{
					return "四通截面与风管截面不匹配！";
				}
				if ((fittingStyle == ConnectDuctStyle.SlantingCrossRound || fittingStyle == ConnectDuctStyle.IntubationCrossRound || fittingStyle == ConnectDuctStyle.RoundAngleCorssRound) && ductShape != 0)
				{
					return "四通截面与风管截面不匹配！";
				}
				List<string> list2 = new List<string>();
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)3, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					list2 = null;
				}
				else
				{
					list2 = connectDuctManage.GetFittingNames(fittingStyle);
				}
				if (fittingStyle <= ConnectDuctStyle.IntubationCross)
				{
					if (fittingStyle == ConnectDuctStyle.RoundAngleCross)
					{
						list2 = connectDuctManage.GetRoundAngleCrossNames();
						hyductConnect.IsConnecteddByCross(ducts, xyzs, list2, ref result);
						goto IL_9DC;
					}
					if (fittingStyle - ConnectDuctStyle.RightAngleCross > 1)
					{
						goto IL_9DC;
					}
				}
				else if (fittingStyle - ConnectDuctStyle.RoundAngleCorssRound > 1)
				{
					if (fittingStyle != ConnectDuctStyle.SlantingCrossRound)
					{
						goto IL_9DC;
					}
					familyInstance = hyductConnect.NewCrossFittingY(ducts, xyzs, list2, ref result);
					goto IL_9DC;
				}
				familyInstance = hyductConnect.NewCrossFitting(ducts, xyzs, list2, ref result);
				IL_9DC:
				if ((familyInstance != null && list2 != null) || list2 == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Reduce:
			{
				string text;
				if (ductShape == 1)
				{
					text = "选择第二个风管(矩形)，与第一个风管在同一直线上";
				}
				else if (ductShape == 0)
				{
					text = "选择第二个风管(圆形)，与第一个风管在同一直线上";
				}
				else
				{
					if (ductShape != 2)
					{
						return "风管类型有误，无法连接！";
					}
					text = "选择第二个风管(椭圆形)，与第一个风管在同一直线上";
				}
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text);
				ducts.Add(this.doc.GetElement(reference) as Duct);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (this.DuctSizeEqual(ducts))
				{
					return "两根风管的尺寸相同，不能进行变径连接！";
				}
				if (ductShape == 1)
				{
					fittingStyle = ConnectDuctStyle.Variable;
				}
				if (ductShape == 0)
				{
					fittingStyle = ConnectDuctStyle.VariableRound;
				}
				if (flag)
				{
					if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)4, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					if (fittingStyle == ConnectDuctStyle.Variable && ductShape != 1)
					{
						return "变径截面与风管截面不匹配！";
					}
					if (fittingStyle == ConnectDuctStyle.VariableRound && ductShape != 0)
					{
						return "变径截面与风管截面不匹配！";
					}
					connectDuctManage = new ConnectDuctManage();
					List<string> fittingNames = connectDuctManage.GetFittingNames(fittingStyle);
					if (fittingNames.Count != 1)
					{
						return "获取变径族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewTransitionFitting(ducts, true, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Round:
			{
				string text;
				if (ductShape == 1)
				{
					text = "选择第二个风管(圆形)，与第一个风管在同一直线上";
				}
				else
				{
					if (ductShape != 0 && ductShape != 2)
					{
						return "风管类型有误，无法连接！";
					}
					text = "选择第二个风管(矩形)，与第一个风管在同一直线上";
				}
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, text);
				ducts.Add(this.doc.GetElement(reference) as Duct);
				if (this.CheckDuctShape(ducts))
				{
					result = "选择的两根风管，必须是一根为矩形风管，一根为圆形风管！";
					return result;
				}
				if (flag)
				{
                    if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)7, 0).MEPPartId) is FamilySymbol))
					{
						return "没有可以使用的族配置，请检查Revit的族配置";
					}
					familySymbol = null;
				}
				else
				{
					connectDuctManage = new ConnectDuctManage();
					List<string> fittingNames = connectDuctManage.GetFittingNames(fittingStyle);
					if (fittingNames.Count != 1)
					{
						return "获取天圆地方族名称失败！";
					}
					familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], this.doc, ref result);
				}
				familyInstance = hyductConnect.NewTransitionFitting(ducts, true, familySymbol, ref result);
				if (familyInstance != null && familySymbol != null)
				{
					familyInstance.Symbol = familySymbol;
					this.doc.Regenerate();
				}
				if (familySymbol == null)
				{
					this.doc.Regenerate();
				}
				break;
			}
			case ConnSelectStyle.Back:
			{
				Reference reference = sel.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "选择第二个风管,与第一根风管平行");
				ducts.Add(this.doc.GetElement(reference) as Duct);
				if (!this.CheckDuctShape(ducts))
				{
					return "选择的两根风管的截面类型必须相同！";
				}
				if (ductShape == 1)
				{
					fittingStyle = ConnectDuctStyle.BackForth;
				}
				if (ductShape == 0)
				{
					fittingStyle = ConnectDuctStyle.BackForthRound;
				}
				if (fittingStyle == ConnectDuctStyle.BackForth && ductShape != 1)
				{
					return "弯头截面与风管截面不匹配！";
				}
				if (fittingStyle == ConnectDuctStyle.BackForthRound && ductShape != 0)
				{
					return "弯头截面与风管截面不匹配！";
				}
				if (!(this.doc.GetElementById(ducts[0].DuctType.RoutingPreferenceManager.GetRule((RoutingPreferenceRuleGroupType)4, 0).MEPPartId) is FamilySymbol))
				{
					return "布管配置中变径配置为空，请检查Revit的族配置";
				}
				connectDuctManage = new ConnectDuctManage();
				List<string> fittingNames = connectDuctManage.GetFittingNames(fittingStyle);
				if (fittingNames.Count != 1)
				{
					return "获取弯头族名称失败！";
				}
				familySymbol = connectDuctManage.GetFamilySymbol(fittingNames[0], this.doc, ref result);
				if (familySymbol == null)
				{
					return result;
				}
				IList<FamilyInstance> list3 = hyductConnect.NewLHWFitting(ducts, true, familySymbol, ref result);
				if (list3 != null && list3.Count > 0)
				{
					this.doc.Regenerate();
				}
				break;
			}
			}
			return result;
		}

		private bool CheckDuctShape(IList<Duct> ducts)
		{
			if (ducts.Count == 2)
			{
				YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
				return hypiping.Shape == hypiping2.Shape;
			}
			if (ducts.Count == 3)
			{
				YJKPiping<Duct> hypiping3 = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping4 = new YJKPiping<Duct>(ducts[1]);
				YJKPiping<Duct> hypiping5 = new YJKPiping<Duct>(ducts[2]);
				return hypiping3.Shape == hypiping4.Shape && hypiping4.Shape == hypiping5.Shape;
			}
			if (ducts.Count == 4)
			{
				YJKPiping<Duct> hypiping6 = new YJKPiping<Duct>(ducts[0]);
				YJKPiping<Duct> hypiping7 = new YJKPiping<Duct>(ducts[1]);
				YJKPiping<Duct> hypiping8 = new YJKPiping<Duct>(ducts[2]);
				YJKPiping<Duct> hypiping9 = new YJKPiping<Duct>(ducts[3]);
				return hypiping6.Shape == hypiping7.Shape && hypiping7.Shape == hypiping8.Shape && hypiping8.Shape == hypiping9.Shape;
			}
			return false;
		}

		private bool DuctSizeEqual(IList<Duct> ducts)
		{
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(ducts[0]);
			YJKPiping<Duct> hypiping2 = new YJKPiping<Duct>(ducts[1]);
			return Math.Abs(hypiping.GetDiameter() - hypiping2.GetDiameter()) < 1E-06;
		}

		private UIApplication uiApp;

		private Document doc;

		private Selection sel;

		public static YJKWindowHandle hWndRevit;
	}
}
